<template>
  <div class="container">
    <div class="search-box">
      <el-input placeholder="请输入内容" v-model="name" class="input-box"></el-input>
      <el-button @click="purge">清除</el-button>
      <el-button type="primary" @click="searchList">搜索</el-button>
    </div>
    <el-table border :data="list" style="width: 100%">
      <el-table-column type="index" :index="indexSort" label="任务编号" width="80">
      </el-table-column>
      <el-table-column prop="name" label="任务名称"> </el-table-column>
      <el-table-column prop="des" label="任务描述"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="{ row }">
          <el-button size="mini" style="margin-right: 10px" @click="detailEdit(row.id)"
            >编辑</el-button
          >
          <el-popconfirm
            confirmButtonText="好的"
            cancelButtonText="不用了"
            icon="el-icon-info"
            iconColor="red"
            title="确定删除任务吗？"
            @confirm="delData(row.id)"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageContainer">
      <el-pagination
        layout="prev, pager, next"
        :current-page="query._page"
        :page-size="query._limit"
        :total="total"
        @current-change="pageChange"
      >
      </el-pagination>
      <el-button size="small" plain @click="addDialog">+ 新增</el-button>
    </div>
    <el-dialog title="添加" :visible="dialogVisible" width="30%" @close="closeDialog">
      <template #default>
        <el-form ref="addForm" :model="addList" :rules="rules" label-width="80px">
          <el-form-item label="任务名称" prop="name">
            <el-input v-model="addList.name"></el-input>
          </el-form-item>
          <el-form-item label="任务描述" prop="des">
            <el-input v-model="addList.des"></el-input>
          </el-form-item>
        </el-form>
      </template>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="confirm">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getListAPI, addDataAPI, getDetailAPI, editDataAPI, delDataAPI, searchListAPI } from '@/api'
import { v4 as uuidv4 } from 'uuid'
export default {
  data() {
    return {
      query: {
        _page: 1, // 当前第几页
        _limit: 5 // 每页条数
      },
      list: [],
      total: 0,
      dialogVisible: false,
      addList: {
        name: '',
        des: '',
        id: ''
      },
      rules: {
        name: [{ required: true, message: '任务名称必填', trigger: 'blur' }],
        des: [{ required: true, message: '任务描述必填', trigger: 'blur' }]
      },
      name: ''
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 获取列表
    async getList() {
      const res = await getListAPI(this.query)
      // console.log(res)
      this.list = res.data
      this.total = JSON.parse(res.headers['x-total-count'])
    },
    // 把当前点击的页数重新赋值给query，并重新发送请求
    pageChange(page) {
      this.query._page = page
      this.getList()
    },
    // 点击添加按钮，打开弹框，添加数据
    addDialog() {
      this.dialogVisible = true
    },
    // 点击x或者空白区域关闭弹框,清空数据
    closeDialog() {
      this.dialogVisible = false
      this.addList = {
        name: '',
        des: '',
        id: ''
      }
      this.$refs.addForm.resetFields()
    },
    // 点击确认进行兜底校验
    confirm() {
      this.$refs.addForm.validate(async valid => {
        if (valid) {
          if (this.addList.id) {
            // 修改
            await editDataAPI(this.addList)
            this.$message.success('修改成功')
          } else {
            // 新增
            const id = uuidv4()
            this.addList.id = id
            // console.log(this.addList)
            await addDataAPI(this.addList)
            this.$message.success('添加成功')
          }
          this.getList()
          this.dialogVisible = false
          this.$refs.addForm.resetFields()
        }
      })
    },
    // 回显数据
    async detailEdit(id) {
      this.dialogVisible = true
      const { data } = await getDetailAPI(id)
      console.log(data)
      this.addList = data
    },
    // 删除数据
    async delData(id) {
      await delDataAPI(id)
      // this.getList()
      this.$message.success('删除成功')
    },
    // 搜索
    async searchList() {
      const { data } = await searchListAPI(this.name)
      this.list = data
    },
    // 清空
    purge() {
      this.name = ''
      this.getList()
    },
    // 序号自增
    indexSort(index) {
      return index + 1 + (this.query._page - 1) * this.query._limit
    }
  }
}
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
